<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/26
 * Time: 11:56
 */

/**
 * Class validation
 * @User: yfl
 * @Time: 2017年9月26日 13:24:09
 * @description: 数据验证类  http://www.111cn.net/phper/php-cy/34007.htm
 */
class validation
{

    public function check_email($value)
    { //check a email address

        return (bool)preg_match('/^[a-z0-9._%+-]+@(?:[a-z0-9-]+.)+[a-z]{2,4}$/i', $value);

    }


    public function check_ip($value)
    { //check a ip address

        return (bool)preg_match("/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){4}$/", $value . ".");

    }


    public function check_date($date, $format = "dd/mm/yy")
    { //check a date

        if (!preg_match("/([0-9]+)([./-])([0-9]+)(\2)([0-9]+)/", $date, $m)) return false;

        $f = explode("/", $format);

        $d[$f[0]] = $m[1];

        $d[$f[1]] = $m[3];

        $d[$f[2]] = $m[5];

        return checkdate($d['mm'], $d['dd'], $d['yyyy'] . $d['yy']);

    }


    public function check_url($url)
    { //check a url

        return (bool)preg_match("/^(?:(?:ht|f)tp(?:s?)://|~/|/)?(?:(?:w+.)+)w+(?::d+)?(?:(?:/[^/?#]+)+)?/?(?:?[^?]*)?(#.*)?$/i", $url);

    }

    //PHP判断远程url是否有效
    public function checkUrlIsGet($url)
    {
        if (!function_exists('get_headers')) return '需要allow_url_fopen=on 开启';
        $array = get_headers($url, 1);
        if (preg_match('/200/', $array[0])) {
//            print_r($array);
            return true;
        } else {
            return false;
        }
    }

    public function isMobile($mobile)
    {
        //preg_match 如果找到一个匹配，preg_match() 函数返回 1，否则返回 0
        if (preg_match("/1[34587]\d{9}$/", $mobile))
            return true;
        else
            return false;
    }

}